<?php
    class Panier {
        //Alias sur table : p
        public $id_panier;
        public $client;
        public $date_ajout;
        public $date_modif;

        //Constructeur complet
        //(Client)client
        public function __construct($id_panier, $client, $date_ajout, $date_modif) {
                $this->setId($id_panier);
                $this->setClient($client);
                $this->setDateAjout($date_ajout);
                $this->setDateModif($date_modif);
        }

        //Ajoute un objet Panier à la BDD
        //Paramètre : aucun
        //Retourne (Panier)
        public function add() {
            $sql =  'INSERT INTO panier (id_client, date_ajout, date_modif) VALUES
                    ('.intval($this->client->getId()).', "'.$this->getDateAjout().'", "'.$this->getDateModif().'")';
            $this->setId(Db::Execute($sql));
            return $this;
        }
        
        //Met à jour un objet Panier dans la BDD
        //Paramètre : aucun
        //Retourne (Panier)
        public function update() {
            $this->setDateModif(new DateTime());
            $sql =  'UPDATE panier
                    SET date_modif = "'.$this->getDateModif().'"
                    WHERE
                        id_panier = '.intval($this->getId());
            Db::Execute($sql);
            return $this;
        }
        
        //Ajoute un objet PanierDetail à la BDD
        //Paramètre : (Produit)produit, (int)quantite, OPTIONNEL (VentePriveeDetail)vente_privee_detail
        //Retourne (PanierDetail)
        public function addPanierDetail($produit, $quantite, $vente_privee_detail = null) {
           $panier_detail = new PanierDetail($this, $produit, $vente_privee_detail, $quantite);
           return $panier_detail->add();
        } 
        
        //Récupère le détail d'un Panier
        //Paramètre : aucun
        //Retourne collection de PanierDetail
        public function getPanierDetails() {
            $sql =  'SELECT
                        pd.quantite as pdquantite, 
                        tv.id_tva as tvid_tva, tv.taux as tvtaux, 
                        ma.id_marque as maid_marque, ma.libelle as malibelle, ma.email as maemail, ma.tel as matel, ma.image as maimage, 
                        ma.date_ajout as madate_ajout, ma.date_modif as madate_modif, ma.valide as mavalide, 
                        prod.id_produit as prodid_produit, prod.libelle as prodlibelle, prod.reference as prodreference, prod.description as proddescription,
                        prod.description_longue as proddescription_longue, prod.prix_unit as prodprix_unit, prod.promotion as prodpromotion, 
                        prod.vente_privee as prodvente_privee, prod.date_ajout as proddate_ajout, prod.date_modif as proddate_modif, 
                        prod.valide as prodvalide, prod.stock as prodstock, 
                        vp.id_vente_privee as vpid_vente_privee, vp.libelle as vplibelle, vp.date_debut as vpdate_debut, 
                        vp.date_fin as vpdate_fin,
                        vpd.id_vente_privee_detail as vpdid_vente_privee_detail, vpd.quantite_restante as vpdquantite_restante, 
                        vpd.promotion as vpdpromotion,
                        tv2.id_tva as tv2id_tva, tv2.taux as tv2taux, 
                        ma2.id_marque as ma2id_marque, ma2.libelle as ma2libelle, ma2.email as ma2email, ma2.tel as ma2tel, 
                        ma2.image as ma2image, ma2.date_ajout as ma2date_ajout, ma2.date_modif as ma2date_modif, ma2.valide as ma2valide, 
                        prod2.id_produit as prod2id_produit, prod2.libelle as prod2libelle, prod2.reference as prod2reference, 
                        prod2.description as prod2description, prod2.description_longue as prod2description_longue, 
                        prod2.prix_unit as prod2prix_unit, prod2.promotion as prod2promotion, prod2.vente_privee as prod2vente_privee, 
                        prod2.date_ajout as prod2date_ajout, prod2.date_modif as prod2date_modif, prod2.valide as prod2valide,
                        prod2.stock as prod2stock
                    FROM panier_detail pd
                    LEFT JOIN vente_privee_detail vpd ON pd.id_vente_privee_detail = vpd.id_vente_privee_detail
                    LEFT JOIN produit prod2 ON vpd.id_produit = prod2.id_produit
                    LEFT JOIN tva tv2 ON prod2.id_tva = tv2.id_tva
                    LEFT JOIN marque ma2 ON prod2.id_marque = ma2.id_marque
                    LEFT JOIN vente_privee vp ON vpd.id_vente_privee = vp.id_vente_privee
                    LEFT JOIN produit prod ON pd.id_produit = prod.id_produit
                    LEFT JOIN tva tv ON prod.id_tva = tv.id_tva
                    LEFT JOIN marque ma ON prod.id_marque = ma.id_marque
                    WHERE
                        pd.id_panier = '.intval($this->getId());
            $ps = Db::Execute($sql, true);
            $tabRetour = array();
            foreach($ps as $p) {
                $vente_privee = new VentePrivee($p['vpid_vente_privee'], $p['vplibelle'], $p['vpdate_debut'], $p['vpdate_fin']);
                $marque = new Marque($p['ma2id_marque'], $p['ma2libelle'], $p['ma2email'], $p['ma2tel'], $p['ma2image'], $p['ma2date_ajout'],
                                    $p['ma2date_modif'], $p['ma2valide']);
                $tva = new Tva($p['tv2id_tva'], $p['tv2taux']);
                $produit = new Produit($p['prod2id_produit'], $marque, $p['prod2libelle'], $p['prod2reference'], $p['prod2description'], 
                                        $p['prod2description_longue'], $p['prod2stock'], $p['prod2prix_unit'], $tva, $p['prod2promotion'], 
                                        $p['prod2vente_privee'], $p['prod2date_ajout'], $p['prod2date_modif'], $p['prod2valide']);
                $vente_privee_detail = new VentePriveeDetail($p['vpdid_vente_privee_detail'], $vente_privee, $produit, 
                                                               $p['vpdquantite_restante'], $p['vpdpromotion']);
                $marque = new Marque($p['maid_marque'], $p['malibelle'], $p['maemail'], $p['matel'], $p['maimage'], $p['madate_ajout'],
                                    $p['madate_modif'], $p['mavalide']);
                $tva = new Tva($p['tvid_tva'], $p['tvtaux']);
                $produit = new Produit($p['prodid_produit'], $marque, $p['prodlibelle'], $p['prodreference'], $p['proddescription'], 
                                        $p['proddescription_longue'], $p['prodstock'], 
                                        $p['prodprix_unit'], $tva, $p['prodpromotion'], $p['prodvente_privee'], $p['proddate_ajout'], 
                                        $p['proddate_modif'], $p['prodvalide']);
                
                $tabRetour[] = new PanierDetail($this, $produit, $vente_privee_detail, $p['pdquantite']);
            }
            
            return $tabRetour;
        }
        
        //Récupère le prix total de l'objet Panier
        //Paramètre : aucun
        //Retourne tableau associatif ([0] => prix_total_ht, [1] => prix_total_ttc)
        public function getTotaux() {
            $panier_details = $this->getPanierDetails();
            $totaux_ht = 0.00;
            $totaux_ttc = 0.00;
            
            foreach($panier_details as $pd) {
                $prix_ht = 0.00;
                $prix_ttc = 0.00;
                if($pd->getVentePriveeDetail() instanceof VentePriveeDetail && $pd->getVentePriveeDetail()->getId() != 0) {
                    $prix_ttc = $pd->getVentePriveeDetail()->getPrixActuel();
                    $prix_ht = $pd->getVentePriveeDetail()->getPrixActuel(false);
                }
                else {
                    $prix_ttc = $pd->getPrix(true);
                    $prix_ht = $pd->getPrix(false);
                }
                
                $totaux_ht += $prix_ht;
                $totaux_ttc += $prix_ttc;
            }
            $tabRetour = array();
            $tabRetour[] = number_format($totaux_ht, 2, '.', '');
            $tabRetour[] = number_format($totaux_ttc, 2, '.', '');
            
            return $tabRetour;
        }
        
        //Crée un objet Commande grâce au Panier
        //Paramètre : (Transporteur)transporteur, (Adresse)adresse, (MoyenPaiement)moyen_paiement, (Avoir)avoir
        //Retourne (Commande)
        public function paiement($transporteur, $adresse, $moyen_paiement, $avoir = NULL) {
            $panier_details = $this->getPanierDetails();
            $totaux = $this->getTotaux();
            $statut = Statut::rechercheStatut(1);

            $frais_transporteur = 0.00;
            if($transporteur->getFrancoPort() > $totaux[0]) {
                $frais_transporteur = $transporteur->getFraisPort();
            }
            
            $commande = new Commande(0, $adresse, $this, $transporteur, $moyen_paiement, $statut, $avoir, $totaux[0], $totaux[1], 
                                    $frais_transporteur, 0, "0000-00-00", new DateTime(), new DateTime());
            return $commande->add();
        }
        
        //Crée un objet Panier grâce à son ID
        //Paramètre : (int)id
        //Retourne (Panier)
        public static function recherchePanier($id) {
            $sql =  'SELECT
                        p.id_panier as pid_panier, p.date_ajout as pdate_ajout, p.date_modif as pdate_modif, 
                        cl.id_client as clid_client, cl.login as cllogin, cl.mdp as clmdp, cl.telephone as cltelephone, cl.mobile as clmobile, 
                        cl.email as clemail, cl.date_naissance as cldate_naissance, cl.date_ajout, cl.date_modif, cl.valide as clvalide, 
                        pro.id_profil as proid_profil, pro.libelle as prolibelle, pro.valide as provalide,
                        civ.id_civilite as civid_civilite, civ.libelle as civlibelle, civ.diminutif as civdiminutif, civ.valide as civvalide
                    FROM panier p
                    LEFT JOIN client cl ON p.id_client = cl.id_client
                    LEFT JOIN profil pro ON cl.id_profil = pro.id_profil
                    LEFT JOIN civilite civ ON cl.id_civilite as civ.id_civilite
                    WHERE
                        p.id_panier = '.intval($id);
            $ps = Db::Execute($sql, true);
            foreach($ps as $p) {
                $profil = new Profil($p['proid_profil'], $p['prolibelle'], $p['provalide']);
                $civilite = new Civilite($p['civid_civilite'], $p['civlibelle'], $p['civdiminutif'], $p['civvalide']);
                $client = new Client($p['clid_client'], $civilite, $profil, $p['cllogin'], $p['clmdp'], $p['clnom'], $p['clprenom'], 
                                    $p['cltelephone'], $p['clmobile'], $p['clemail'], $p['cldate_naissance'], $p['cldate_ajout'], 
                                    $p['cldate_modif'], $p['clvalide']);
                return new Panier($p['pid_panier'], $client, $p['pdate_ajout'], $p['pdate_modif']);
            }

            return null;
        }
		
        //GETTERS ET SETTERS
        public function getId() {return $this->id_panier;}
        public function getClient() {return $this->client;}
        public function getDateAjout($format = "Y-m-d H:i:s") {return Tools::getterDate($this->date_ajout, $format);}
        public function getDateModif($format = "Y-m-d H:i:s") {return Tools::getterDate($this->date_modif, $format);}

        public function setId($temp) {$this->id_panier = intval($temp);}
        public function setClient($temp) {$this->client = $temp;}
        public function setDateAjout($temp) {$this->date_ajout = Tools::setterDate($temp);}
        public function setDateModif($temp) {$this->date_modif = Tools::setterDate($temp);}
    }

?>